<?php
namespace app\admin\server\trading;
use app\admin\ServerBase;
use think\Request;
use think\facade\Db;
use app\admin\common;
use app\admin\model\trading\OrderAudit as OrderAuditModel;
use app\admin\model\log\Log;

class OrderAudit extends ServerBase{
	protected static $s_model=[];
	public $param;
	public function __construct( Request $request )
	{
    	$this->checkAuth();
        $this->REQUEST = $request;
        $this->param=$request->param();
        self::$s_model=new OrderAuditModel();
    }
    
    public function checkselectAuditOrder(){
    	$_limit=$this->getPage($this->param);
    	$_field="n.id,u.nickname,u.mobile,o.title,n.status,n.each_price,n.is_commission,n.online_time,n.number,s.audit_money,u.true_name,u.card_id";
    	$_order="online_time asc";
    	if(isset($this->param['mobile']) && $this->param['mobile']!=''){
    		$_where[]=["u.mobile","like",'"%'.$this->param['mobile'].'%"'];
    	}
    	if(isset($this->param['nickname']) && $this->param['nickname']!=''){
    		$_where[]=["u.nickname","like",'"%'.$this->param['nickname'].'%"'];
    	}
    	if(isset($this->param['title']) && $this->param['title']!=''){
    		$_where[]=["o.title","like",'"%'.$this->param['title'].'%"'];
    	}
    	if(isset($this->param['number']) && $this->param['number']!=''){
    		$_where[]=["n.number","like",'"%'.$this->param['number'].'%"'];
    	}
    	//只查询当前是待审核的数据
    	$_where[]=["n.status","=",3];
    	return ['_where'=>$_where,"_field"=>$_field,"_limit"=>$_limit,"_order"=>$_order];
    }
    
   	public function selectAuditOrder(){
   		$_extract=$this->checkselectAuditOrder();
   		extract($_extract);
   		$_count=self::$s_model->findAuditOrder($_where,"count(o.id) num");
   		if(!$_count || $_count['num']<=0){
   			$this->getMessageCode('adminMessage.success.empty',1);
    		return [];
   		}
   		$_data=self::$s_model->selectAuditOrder($_where,$_field,$_limit,$_order);
   		foreach($_data as $k=>$v){
   			$_data[$k]['online_time']=date('Y-m-d H:i:s',$v['online_time']);
   		}
   		$result['data']=$_data;
		$result['count']=$this->getPageCount($_count['num']);
    	$this->getMessageCode('adminMessage.success.select',2);
    	return $result;	
   	}
   
   
    public function audit(){
    	$_param=$this->REQUEST->param();
    	$_param['ids']=explode(',',$_param['ids']);
    	//1.查找当前数据是否有已被审核的
    	$_where[]=["id","in",$_param['ids']];
    	$_where[]=["status","<>",3];
    	$_audit_data=self::$s_model->selectNumberAuditStatus($_where,"id,status");
    	if(!empty($_audit_data)){
    		$this->getMessageCode('adminMessage.error.auth',1);
    		$_error_ids=implode(',',array_column($_audit_data,'id'));
    		$this->ERROR.=",当前提交数据有非待审核数据 id为:".$_error_ids;
    		return false;
    	}
    	Db::startTrans();
    	$_where_audit[]=["id","in",$_param['ids']];
    	if(!self::$s_model->updateData(['status'=>1],$_where_audit)){
    		$this->getMessageCode('adminMessage.error.auth',1);
    		Db::rollback();
    		return false;
    	}
    	$_where=array();
    	$_where[]=["id","in",$_param['ids']];
    	$_where[]=["status","<>",1];
    	$_audit_data=self::$s_model->selectNumberAuditStatus($_where,"id,status");
    	if(!empty($_audit_data)){
    		$this->getMessageCode('adminMessage.error.auth',1);
    		$_error_ids=implode(',',array_column($_audit_data,'id'));
    		$this->ERROR.=",提交审核失败id为:".$_error_ids;
    		Db::rollback();
    		return false;
    	}
    	$_audit_log=[
    		'ids'=>implode(',',$_param['ids']),
    		'uid'=>$this->getAdmin()['id'],
    		'create_time'=>time(),
    	];
    	if(!((new Log())->insertAuditLog($_audit_log))){
    		$this->getMessageCode('adminMessage.error.auth',1);
    		$this->ERROR.=",记录日志失败";
    		return false;
    	}
    	Db::commit();
    	$this->getMessageCode('adminMessage.success.auth',2);
    	return true;
    }
    /**
     * @title 拒绝
     * 
     */
    public function refused(){
    	$_param=$this->REQUEST->param();
    	$_param['id']=explode(',',$_param['id']);
    	//1.查找当前数据是否有已被审核的
    	$_where[]=["id","in",$_param['id']];
    	$_where[]=["status","<>",3];
    	$_audit_data=self::$s_model->selectNumberAuditStatus($_where,"id,status");
    	if(!empty($_audit_data)){
    		$this->getMessageCode('adminMessage.error.rejected',1);
    		$_error_ids=implode(',',array_column($_audit_data,'id'));
    		$this->ERROR.=",当前提交数据有非待审核数据 id为:".$_error_ids;
    		return false;
    	}
    	Db::startTrans();
    	$_where_audit[]=["id","in",$_param['id']];
    	if(!self::$s_model->updateData(['status'=>-1,'update_time'=>time()],$_where_audit)){
    		$this->getMessageCode('adminMessage.error.rejected',1);
    		Db::rollback();
    		return false;
    	}
    	$_audit_log=[
    		'ids'=>implode(',',$_param['id']),
    		'uid'=>$this->getAdmin()['id'],
    		'create_time'=>time(),
    		'type'=>1,
    	];
    	if(!((new Log())->insertAuditLog($_audit_log))){
    		$this->getMessageCode('adminMessage.error.rejected',1);
    		$this->ERROR.=",记录日志失败";
    		return false;
    	}
    	Db::commit();
    	$this->getMessageCode('adminMessage.success.rejected',2);
    	return true;
    }
    
}
?>